﻿Imports Oracle.DataAccess.Client
Imports lbGlobalcom
Public Class fListaTipoDocs
    Dim daListaTipoDoc As lbGlobalcom.dsConsultasGlobalTableAdapters.GLO_LISTATIPOSDOCTableAdapter
    Dim daDatosTablas As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter

    Private lIdCcosto As String
    Private lTipoDoc As String
    Private lBodegaPara As String
    Private lBodegaDe As String
    Private lBodegaEs As Integer = -1
    Private lSubClase As String = ""
    Private lDocEs As String = ""
    Public Property DocEs() As String
        Get
            Return lDocEs
        End Get
        Set(ByVal value As String)
            lDocEs = value
        End Set
    End Property
    Public Property SubClase() As String
        Get
            Return lSubClase
        End Get
        Set(ByVal value As String)
            lSubClase = value
        End Set
    End Property
    Public ReadOnly Property idTipoDoc() As String
        Get
            Return lTipoDoc
        End Get
    End Property
    Public ReadOnly Property Ccosto() As String
        Get
            Return lIdCcosto
        End Get
    End Property

    Public ReadOnly Property BodegaP() As String
        Get
            Return lBodegaPara
        End Get
    End Property

    Public ReadOnly Property BodegaD() As String
        Get
            Return lBodegaDe
        End Get
    End Property

    Public Property BodegaEs() As Integer
        Get
            Return lBodegaEs
        End Get
        Set(ByVal value As Integer)
            lBodegaEs = value
        End Set
    End Property


    Private Sub fListaTipoDocs_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        daListaTipoDoc = New lbGlobalcom.dsConsultasGlobalTableAdapters.GLO_LISTATIPOSDOCTableAdapter
        daDatosTablas = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
        Try
            If lSubClase.Trim.Length > 0 Then
                daDatosTablas.FillByIdGrupoSubclase(DsGlobal.GLO_DATOSTABLAS, "td", lSubClase)
            Else
                daDatosTablas.FillByIdGrupo(DsGlobal.GLO_DATOSTABLAS, "td")
            End If
            Dim dr As lbGlobalcom.dsGlobal.GLO_DATOSTABLASRow = DsGlobal.GLO_DATOSTABLAS.NewGLO_DATOSTABLASRow
            dr.DESCRIPCION = "Todas"
            dr.IDGRUPO = "td"
            dr.ID = -1
            DsGlobal.GLO_DATOSTABLAS.Rows.Add(dr)
            dr = Nothing
            Me.cbDocEs.SelectedValue = lBodegaEs
            If lDocEs.Trim.Length > 0 Then
                cbDocEs.Text = lDocEs
                cbDocEs.Enabled = False
            End If
            If lBodegaEs <> -1 Then
                Me.cbDocEs.Enabled = False
                Me.btnConsulta.PerformClick()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub


    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnConsulta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsulta.Click
        Dim conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = conn.CreateCommand
        Dim qWhere As String = "WHERE GLO_TIPODOCUMENTO.DOCES = GLO_DATOSTABLAS.ID AND (GLO_DATOSTABLAS.IDGRUPO = 'td') "
        cmd.CommandText = "SELECT GLO_TIPODOCUMENTO.TIPODOC, GLO_TIPODOCUMENTO.DESCRIPCION, GLO_TIPODOCUMENTO.DOCES, " & _
                                "GLO_TIPODOCUMENTO.IDCCOSTO,GLO_TIPODOCUMENTO.BODEGAPARA,GLO_TIPODOCUMENTO.BODEGADE,GLO_DATOSTABLAS.DESCRIPCION AS DESCRIPCIONDOCES " & _
                                "FROM GLO_TIPODOCUMENTO, GLO_DATOSTABLAS "
        qWhere &= " AND GLO_TIPODOCUMENTO.IDSEDE = " & CustomPrincipal.GetSede()
        If Me.txtNombreTipoDoc.Text.Trim.Length > 0 Then
            qWhere &= " AND GLO_TIPODOCUMENTO.DESCRIPCION LIKE '%" & Me.txtNombreTipoDoc.Text & "%'"
        End If
        If Me.cbDocEs.SelectedValue <> -1 Then
            qWhere &= " AND GLO_TIPODOCUMENTO.DOCES = " & Me.cbDocEs.SelectedValue
        ElseIf SubClase.Trim.Length > 0 Then
            qWhere &= " AND GLO_DATOSTABLAS.SUBCLASE='" & SubClase & "'"
        End If
        If SubClase.Length > 0 Then
            Dim filtroDoc As String = CustomPrincipal.GetFiltroTiposDoc("GLO_TIPODOCUMENTO.TIPODOC")
            If filtroDoc.Length > 0 Then
                qWhere &= " AND " & filtroDoc
            End If
        End If
        cmd.CommandText &= qWhere
        Try
            conn.Open()
            DsConsultasGlobal.GLO_LISTATIPOSDOC.Rows.Clear()
            DsConsultasGlobal.GLO_LISTATIPOSDOC.Load(cmd.ExecuteReader)
            conn.Close()
            GLO_LISTATIPOSDOCDataGridView.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        Finally
            conn.Close()
        End Try
    End Sub


    Private Sub GLO_LISTATIPOSDOCDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles GLO_LISTATIPOSDOCDataGridView.CellMouseDoubleClick
        AsignaTipoDoc(e.RowIndex)
    End Sub

    Private Sub AsignaTipoDoc(ByVal Fila As Integer)
        Try
            If Fila >= 0 Then
                lTipoDoc = GLO_LISTATIPOSDOCDataGridView.Rows(Fila).Cells("TIPODOC").Value
                If Not (IsDBNull(GLO_LISTATIPOSDOCDataGridView.Rows(Fila).Cells("IDCCOSTO").Value)) Then
                    lIdCcosto = GLO_LISTATIPOSDOCDataGridView.Rows(Fila).Cells("IDCCOSTO").Value
                End If
                If Not (IsDBNull(GLO_LISTATIPOSDOCDataGridView.Rows(Fila).Cells("BODEGAPARA").Value)) Then
                    lBodegaPara = GLO_LISTATIPOSDOCDataGridView.Rows(Fila).Cells("BODEGAPARA").Value
                End If
                If Not (IsDBNull(GLO_LISTATIPOSDOCDataGridView.Rows(Fila).Cells("BODEGADE").Value)) Then
                    lBodegaDe = GLO_LISTATIPOSDOCDataGridView.Rows(Fila).Cells("BODEGADE").Value
                End If
                Me.DialogResult = Windows.Forms.DialogResult.OK
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
        
    End Sub
    Private Sub GLO_LISTATIPOSDOCDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GLO_LISTATIPOSDOCDataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaTipoDoc(GLO_LISTATIPOSDOCDataGridView.CurrentRow.Index)
        End If
    End Sub
End Class